package vip.specimen.system.controller.resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemResourceEntity;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemResourceKindService;
import vip.specimen.system.service.SystemResourceService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/16 16:52
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "资源管理")
@RestController
@RequestMapping(value = "resource")
public class ResourceController {
    @Resource
    private SystemResourceKindService resourceKindService;
    @Resource
    private SystemResourceService resourceService;

    @GetMapping
    @RequiresPermissions(value = "resource:select")
    @ApiOperation(value = "查看", notes = "根据资源分类uuid获取资源列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemResourceEntity>> get(@RequestParam("uuid") UUID uuid) {
        return JsonResult.success(this.resourceKindService.findKindResource(uuid));
    }

    @DeleteMapping("batch")
    @RequiresPermissions(value = "resource:delete:batch")
    @SystemJurisdiction(resourceType = "button", permissionName = "批量删除资源")
    @ApiOperation(value = "批量删除", notes = "根据uuid列表批量删除资源")
    public JsonResult<HashMap<String, Integer>> deletes(@RequestBody HashMap<String, Set<UUID>> param) {
        int result = this.resourceService.delete(param.get("uuids"));
        return JsonResult.success(new HashMap<String, Integer>(1) {{
            put("successNum", result);
        }});
    }

    @DeleteMapping
    @RequiresPermissions(value = "resource:delete:delete")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除资源")
    @ApiOperation(value = "删除", notes = "根据uuid删除资源")
    public JsonResult delete(@RequestBody HashMap<String, String> param) {
        return this.resourceService.delete(param.get("name")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }
}
